﻿
/**
 * 验证form
 * @param  f1 js form 对象
 * @returns true or false 
 */
function formCheckValidity(f1) {
    var fd = new FormData(f1);
    for (var k of fd.keys()) {
        if (f1[k].checkValidity()) {
            $(f1[k]).parents('.form-group').removeClass('has-error');
        } else {
            $(f1[k]).parents('.form-group').addClass('has-error');
        }
    }
    return f1.checkValidity();
}

/**
 * 把form 转化为 Json
 * @param {}  js form 对象 或jquery选择器
 * @returns {} Json格式数据
 */
function toJson(f1) {
    var data = {};
    var arr = $(f1).serializeArray();
    for (var i = 0; i < arr.length; i++) {
        var item = arr[i];
        data[item.name] = item.value;
    }
    return data;
}

function requestFai(r) {
    if (r.code === 1) {
        location.href = '/home/login';
        return;
    }
    if (r.code === 2) {
        alert('无效的请求参数 ' + r.msg);
        return;
    }
    if (r.code === 3) {
        alert('处理失败 ' + r.msg);
        return;
    }
    if (r.code === 4) {
        alert('服务器错误 ' + r.msg);
        return;
    }
}

/**
 * 分页
 * @param {} rowCount 总行数 
 * @param {} limit 页长
 * @param {} page 页码
 * @returns {} html字符
 */
function fnInitPage(rowCount, limit, page) {
    var totalPage = rowCount / limit;
    if (totalPage <= 1) {
        return "";
    }
    var str = "<ul class='pagination'>";
    if (page > 1) {
        str += "<li><a href='javascript:goto(" + (page - 1) + ")' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>";
    }
    if (page - 3 > 0) {
        str += "<li><a href='javascript:goto(" + (page - 3) + ")' >" + (page - 3) + "</a></li>";
    }
    if (page - 2 > 0) {
        str += "<li><a href='javascript:goto(" + (page - 2) + ")' >" + (page - 2) + "</a></li>";
    }
    if (page - 1 > 0) {
        str += "<li><a href='javascript:goto(" + (page - 1) + ")' >" + (page - 1) + "</a></li>";
    }
    str += "<li class='active'><a href='javascript:goto(" + page + ")' >" + page + "</a></li>";

    if (page < totalPage) {
        str += "<li><a href='javascript:goto(" + (page + 1) + ")' >" + (page + 1) + "</a></li>";
    }
    if (page + 1 < totalPage) {
        str += "<li><a href='javascript:goto(" + (page + 2) + ")' >" + (page + 2) + "</a></li>";
    }
    if (page + 2 < totalPage) {
        str += "<li><a href='javascript:goto(" + (page + 3) + ")' >" + (page + 3) + "</a></li>";
    }

    if (page + 3 < totalPage) {
        str += "<li><a href='javascript:goto(" + (page + 4) + ")' >" + (page + 4) + "</a></li>";
    }
    if (page < totalPage) {
        str += "<li><a href='javascript:goto(" + (page + 1) + ")' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>";
    }
    str += "</ul>";
    return str;
}

/**
 * 上传文件
 * @param {} f 
 * @param {} callback 成功回调 fn(url) 
 * @returns {} 
 */
function fnUploadFile(f, callback) {
    var fd = new FormData();
    fd.append(f.name, f.files[0]);
    $.ajax({
        url: "/img/uplaod",
        enctype: 'multipart/form-data',
        processData: false,
        contentType: false,
        data: fd,
        type: "POST",
        success: function (r) {
            if (r.code === 0) {
                callback( r.data);
            } else {
                requestFai(r);
            }
        },
        error: function (r) {
            alert("ERROR: " + JSON.stringify(r));
        }
    });
}

/**
 * 预览图片
 * @param {} e 
 * @returns {} 
 */
function prviewImg(e) {
    var url = $(e).attr("data-url");
    if (url) {
        //
        var p = document.getElementById("popover_prview_009009009");
        if (p) {
            //console.log("has");
            p.remove();
        }

        var html =
            "<div id='popover_prview_009009009' class='popover fade in prview' role='tooltip'  onresize='previewResize(this);'><div class='arrow'></div>" + "<div class='prview-title'><button class='close' onclick='previewClose(this);'><span aria-hidden='true'>&times;</span></button></div>" + "<div class='popover-content'><img src='" + url + "' class='img-rounded' style='max-width:400px; max-height:400px;' />" + "</div></div>";
        $("body").append(html);
        p = document.getElementById("popover_prview_009009009");

        var rect = e.getBoundingClientRect();
        p.style.left = rect.left + 'px';
        p.style.top = rect.bottom + 'px';

        var id = setInterval(previewResize, 100);

        p.setAttribute("timeInterval", id);
        p.setAttribute("eventLeft", rect.left);
        p.setAttribute("eventRight", rect.right);
        p.setAttribute("eventTop", rect.top);
        p.setAttribute("eventBottom", rect.bottom);

    }
}

/**
 * 关闭预览图片
 * @param {} e 
 * @returns {} 
 */

function previewClose(e) {
    var p = e.parentNode.parentNode;
    var id = p.getAttribute("timeInterval");
    clearInterval(id);
    p.parentNode.removeChild(p);
}

/**
 * 调整预览图片位置
 * @returns {} 
 */
function previewResize() {
    var p = document.getElementById('popover_prview_009009009');
    if (!p) {
        return;
    }
    var sLeft = Number(p.getAttribute("eventLeft"));
    var sRight = Number(p.getAttribute("eventRight"));
    var sTop = Number(p.getAttribute("eventTop"));
    var sBottom = Number(p.getAttribute("eventBottom"));
    var rect = p.getBoundingClientRect();
    //p.classList.remove(['top', 'bottom', 'right', 'left']);
    var b = document.getElementsByTagName('body')[0].getBoundingClientRect();
    var w = b.width;
    var h = b.height;
    //console.log('rect w:' + rect.width + ', h:' + rect.height + ', sBottom' + sBottom +', body.h:' + h);
    var cw = (sLeft + sRight) / 2;
    var ch = (sTop + sBottom) / 2;
    if (w - sRight - rect.width > 0) {
        //right
        //console.log('right');
        p.classList.add('right');
        p.style.top = ch - rect.height / 2 + 'px';
        p.style.left = sRight + 'px';
        return;
    }
    if (sLeft - rect.width > 0) {
        //left
        p.classList.add('left');
        p.style.left = sLeft - rect.width + 'px';
        p.style.top = ch - rect.height / 2 + 'px';
        return;
    }
    if (sBottom + rect.height < h) {
        //bottom
        p.classList.add('bottom');
        p.style.left = cw - (rect.width / 2) + 'px';
        p.style.top = sBottom + 'px';
        return;
    }
    if (sTop - rect.height > 0) {
        //top
        p.classList.add('top');
        p.style.left = cw - (rect.width / 2) + 'px';
        p.style.top = (sTop - rect.height) + 'px';
        return;
    }

}
